package morenoapp.components.input;

import javax.swing.event.TableModelEvent;
import morenoapp.components.common.AbstractCustomColumnsMorenoTableModel;
import morenoapp.components.common.table.column.ColumnMeta;
import morenoapp.components.common.table.column.MorenoColumnsMeta;
import morenoapp.model.enums.MorenoRespondentsSortOrder;
import morenoapp.model.listeners.MorenoModelAdapter;
import morenoapp.model.data.MorenoRespondentFace;

/**
 *
 * 
 * @author dmitry.mamonov
 */
public class InputRespondentsTableModel extends AbstractCustomColumnsMorenoTableModel {

    private final ColumnMeta[] FIRST_COLUMNS_META = new ColumnMeta[]{
        MorenoColumnsMeta.createColumnMetaNumber(),
        MorenoColumnsMeta.createColumnMetaName(true),
        MorenoColumnsMeta.createColumnMetaSexMale(),
        MorenoColumnsMeta.createColumnMetaSexFemale(),
        MorenoColumnsMeta.createColumnMetaPassedPoll(),
    };

    public InputRespondentsTableModel() {
        super();
        getMorenoModel().addModelListener(new ModelDiligentListener());
    }

    @Override
    protected ColumnMeta getFirstColumnMeta(int index) {
        return FIRST_COLUMNS_META[index];
    }

    @Override
    protected int getFirstColumnsCount() {
        return FIRST_COLUMNS_META.length;
    }

    @Override
    protected ColumnMeta getLastColumnMeta(int index) {
        throw new UnsupportedOperationException("No last column"); //NOI18N
    }

    @Override
    protected int getLastColumnsCount() {
        return 0; //no last columns
    }

    @Override
    protected int getMiddleConlumnsCount() {
        return 0; //no middle part
    }

    @Override
    protected ColumnMeta getMiddleColumnMeta(int index) {
        throw new UnsupportedOperationException("No middle column"); //NOI18N
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return getColumnMeta(columnIndex).isEditable();
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        ColumnMeta columnMeta = getColumnMeta(columnIndex);
        if (columnMeta.isEditable()) {
            columnMeta.setRowValue(rowIndex, aValue);
        } else {
            throw new RuntimeException("Editing illegal (row, column) " + rowIndex + "/" + columnIndex); //NOI18N
        }
    }

    @Override
    public int getRowCount() {
        return getMorenoModel().getRespondentsList().size();
    }

    MorenoRespondentFace getRespondentAtRow(int row) {
        return getMorenoModel().getRespondentsList().get(row);
    }

    class ModelDiligentListener extends MorenoModelAdapter {
        @Override
        public void onRespondentChanged(MorenoRespondentFace changed) {
            fireTableChanged(new TableModelEvent(InputRespondentsTableModel.this, changed.getIndex()));
        }

        @Override
        public void onRespondentsListChanged() {
            fireTableChanged(new TableModelEvent(InputRespondentsTableModel.this));
        }
    }
}
